<?php

namespace App\Services\Statistics;

use AlibabaCloud\SDK\Dingtalk\Vdrive_1_0\Models\GetPreviewInfoResponseBody\info;
use Carbon\Carbon;
use App\Models\User;
use App\Models\Order;
use App\Models\Comment;
use App\Models\Product;
use App\Models\ProductSku;
use App\Models\GoldCharge;
use App\Models\GoldRecord;
use App\Models\OrderRefund;

class HardCount
{
	/*---------- gold charges ------------------------------------------------------------------------------------------*/

	/** total_gold_charges_money_now */
	public function totalGoldChargesMoneyNow()
	{
		return GoldCharge::query()
			->whereNotNull('paid_at')
			->sum('total_amount');
	}

	/** total_gold_charges_money_today */
	public function totalGoldChargesMoneyToday()
	{
		return GoldCharge::query()
			->whereDate('created_at', Carbon::today())
			->whereNotNull('paid_at')
			->sum('total_amount');
	}

	/*---------- gold beans --------------------------------------------------------------------------------------------*/

	/** total_gold_beans_fee_now */
	public function totalGoldBeansFeeNow()
	{
		return GoldRecord::query()
			->whereNotNull('paid_method')
			->whereNotNull('paid_at')
			->sum('pay_gold_beans');
	}

	/** total_gold_beans_fee_today */
	public function totalGoldBeansFeeToday()
	{
		return GoldRecord::query()
			->whereDate('created_at', Carbon::today())
			->whereNotNull('paid_method')
			->whereNotNull('paid_at')
			->sum('pay_gold_beans');
	}

	/*---------- gold records ------------------------------------------------------------------------------------------*/


	/** total_gold_records_money_now */
	public function totalGoldRecordsMoneyNow()
	{
		return GoldRecord::query()
			->whereNotNull('paid_at')
			->sum('pay_net_price');
	}

	/** total_gold_records_money_today */
	public function totalGoldRecordsMoneyToday()
	{
		return GoldRecord::query()
			->whereDate('created_at', Carbon::today())
			->whereNotNull('paid_at')
			->sum('pay_net_price');
	}

	/** total_gold_records_count_now */
	public function totalGoldRecordsCountNow()
	{
		return GoldRecord::query()
			->whereNotNull('paid_at')
			->count();
	}

	/** total_gold_records_count_today */
	public function totalGoldRecordsCountToday()
	{
		return GoldRecord::query()
			->whereDate('created_at', Carbon::today())
			->whereNotNull('paid_at')
			->count();
	}

	/** total_gold_records_count_back1_day */
	public function totalGoldRecordsCountBack1Day()
	{
		return GoldRecord::query()
			->whereDate('created_at', Carbon::today()->addDays(-1))
			->whereNotNull('paid_at')
			->count();
	}

	/** total_gold_records_count_back2_day */
	public function totalGoldRecordsCountBack2Day()
	{
		return GoldRecord::query()
			->whereDate('created_at', Carbon::today()->addDays(-2))
			->whereNotNull('paid_at')
			->count();
	}

	/** total_gold_records_count_back3_day */
	public function totalGoldRecordsCountBack3Day()
	{
		return GoldRecord::query()
			->whereDate('created_at', Carbon::today()->addDays(-3))
			->whereNotNull('paid_at')
			->count();
	}

	/** total_gold_records_count_back4_day */
	public function totalGoldRecordsCountBack4Day()
	{
		return GoldRecord::query()
			->whereDate('created_at', Carbon::today()->addDays(-4))
			->whereNotNull('paid_at')
			->count();
	}

	/*---------- gold bonuses ------------------------------------------------------------------------------------------*/


	/** total_gold_bonus_money_now */
	public function totalGoldBonusMoneyNow()
	{
		return User::all()->sum('dividend_all');
	}

	/** total_gold_bonus_money_today  */
	public function totalGoldBonusMoneyToday()
	{
		return User::all()->sum('dividend_day');
	}

	/*---------- products ----------------------------------------------------------------------------------------------*/


	/** total_product_spus_now */
	public function totalProductSpusNow(): int
	{
		return Product::count();
	}

	/** total_product_skus_now */
	public function totalProductSkusNow(): int
	{
		return ProductSku::count();
	}


	/*---------- product orders ----------------------------------------------------------------------------------------*/

	/** total_product_orders_count_now */
	public function totalProductOrdersCountNow(): int
	{
		return Order::query()->whereDate('created_at', '>', '2022-08-21')->count();
	}

	/** total_product_orders_money_now */
	public function totalProductOrdersMoneyNow()
	{
		return Order::query()
            ->whereDate('created_at', '>', '2022-08-21')
            ->whereNotNull('paid_at')
            ->sum('total_amount');
	}

	/** total_product_orders_profit_now */
	public function totalProductOrdersProfitNow()
	{
		return Order::query()->whereDate('created_at', '>', '2022-08-21')
            ->whereNotNull('paid_at')
            ->sum('total_profit');
	}

	/** total_product_orders_refund_now */
	public function totalProductOrdersRefundNow()
	{
		return OrderRefund::query()
            ->whereDate('created_at', '>', '2022-08-21')
            ->where('refund_status', 'succeeded')
            ->sum('refund_money');
	}


	/*---------- comments ----------------------------------------------------------------------------------------------*/


	/** total_comments_now */
	public function totalCommentsNow()
	{
		return Comment::count();
	}


	/*---------- users -------------------------------------------------------------------------------------------------*/


	/** total_users_count_now */
	public function totalUsersCountNow()
	{
		return User::select()->count();
	}

	/** total_normal_users_count_now */
	public function totalNormalUsersCountNow()
	{
		return User::select()->where('grade', 'normal')->count();
	}

	/** total_normal_users_count_today */
	public function totalNormalUsersCountToday()
	{
		return User::select()->where('grade', 'normal')
			->whereDate('created_at', Carbon::today())
			->count();
	}

	/** total_normal_users_count_back1_day */
	public function totalNormalUsersCountBack1Day()
	{
		return User::select()->where('grade', 'normal')
			->whereDate('created_at', Carbon::today()->addDays(-1))
			->count();
	}

	/** total_normal_users_count_back2_day */
	public function totalNormalUsersCountBack2Day()
	{
		return User::select()->where('grade', 'normal')
			->whereDate('created_at', Carbon::today()->addDays(-2))
			->count();
	}

	/** total_normal_users_count_back3_day */
	public function totalNormalUsersCountBack3Day()
	{
		return User::select()->where('grade', 'normal')
			->whereDate('created_at', Carbon::today()->addDays(-3))
			->count();
	}

	/** total_normal_users_count_back4_day */
	public function totalNormalUsersCountBack4Day()
	{
		return User::select()->where('grade', 'normal')
			->whereDate('created_at', Carbon::today()->addDays(-4))
			->count();
	}

	/** total_vip_users_count_now */
	public function totalVipUsersCountNow()
	{
		return User::select()->where('grade', 'vip')->count();
	}

	/** total_vip_users_count_today */
	public function totalVipUsersCountToday()
	{
		return User::select()->where('grade', 'vip')
			->whereDate('created_at', Carbon::today())
			->count();
	}

	/** total_vip_users_count_back1_day */
	public function totalVipUsersCountBack1Day()
	{
		return User::select()->where('grade', 'vip')
			->whereDate('created_at', Carbon::today()->addDays(-1))
			->count();
	}

	/** total_vip_users_count_back2_day */
	public function totalVipUsersCountBack2Day()
	{
		return User::select()->where('grade', 'vip')
			->whereDate('created_at', Carbon::today()->addDays(-2))
			->count();
	}

	/** total_vip_users_count_back3_day */
	public function totalVipUsersCountBack3Day()
	{
		return User::select()->where('grade', 'vip')
			->whereDate('created_at', Carbon::today()->addDays(-3))
			->count();
	}

	/** total_vip_users_count_back4_day */
	public function totalVipUsersCountBack4Day()
	{
		return User::select()->where('grade', 'vip')
			->whereDate('created_at', Carbon::today()->addDays(-4))
			->count();
	}
}
